package com.cip.master.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;

import org.apache.velocity.app.VelocityEngine;

import com.cip.common.util.CommonUtil;
import com.cip.common.util.GridUtil;
import com.cip.master.dao.PaymentDAO;
import com.cip.master.model.Payment;
import com.cip.master.service.PaymentService;

public class PaymentServiceImpl implements PaymentService {
	
	private PaymentDAO paymentDAO;
	private VelocityEngine velocityEngine;
	
	public String getPaymentGridList(Locale locale,String username)throws Exception {
		
		String gridSource = ""; 
		
		try {
			
			List<Payment> paymentList = getAllPaymentsByUser(username);
			
			List<Map<String,Object>> paymentArrayList = new ArrayList<Map<String,Object>>();
			
			for (Payment payment : paymentList) {
				Map<String,Object> paymentObj = new HashMap<String, Object>();
				paymentObj.put("paidFor",payment.getPaidFor());
				paymentObj.put("paidAmount", payment.getPaidAmount());
				paymentObj.put("paymentDate", payment.getPaymentDate());
				paymentObj.put("status", payment.getStatus());
				
				paymentArrayList.add(paymentObj);			
			}
			
			gridSource = generateScriptForProcess(velocityEngine, paymentArrayList, locale);
			
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return gridSource;
	}
	
	/**
	 * Set into context of grid column names and field names and it attributes
	 * 
	 * @param context
	 * @return
	 * @throws Exception
	 */
	public static String generateScriptForProcess(VelocityEngine velocityEngine, List<Map<String, Object>> paymentArrayList, Locale locale) throws Exception {
		
		return GridUtil.generateScriptForProcess(velocityEngine, paymentArrayList, createPaymentGridColumnMapping(locale));
	}
	
	/**
	 * Add needed column and it mapping for my instances
	 * 
	 * @param context
	 */
	private static Map<String, Object> createPaymentGridColumnMapping(Locale locale) {
		Map<String, Object> context = new HashMap<String, Object>();
		List<Map<String, Object>> fieldNameList = new ArrayList<Map<String, Object>>();
		//ResourceBundle appResourceBundle = ResourceBundle.getBundle(Constants.BUNDLE_KEY ,locale);
		//context.put("title", appResourceBundle.getString("process.myInstances"));
		context.put("title", "Payment List");
		context.put("gridId", "MY_PAYMENTLIST");
		context.put("needTreeStructure", true);
		//context.put("dynamicGridWidth", "300px");
		//context.put("dynamicGridHeight", "580px");
		String columnNames = "['Paid For','Paid Amount','Date','Status']";
        CommonUtil.createFieldNameList(fieldNameList, "paidFor", "100", "center", "","false");
        CommonUtil.createFieldNameList(fieldNameList, "paidAmount", "100", "left","", "false");
        CommonUtil.createFieldNameList(fieldNameList, "paymentDate", "100", "left","", "false");
    	CommonUtil.createFieldNameList(fieldNameList, "status", "100", "center", "", "false");

        context.put("columnNames", columnNames);
        context.put("noOfRecords", "10");
		context.put("fieldNameList", fieldNameList);
		context.put("gridHeading", "true");
		
		return context;
	}

	public Payment savePayment(Payment payment) {
		return paymentDAO.savePayment(payment);
	}

	public Payment getPaymentById(int id) throws Exception{
		return paymentDAO.getPaymentById(id);
	}
	public void setPaymentDAO(PaymentDAO paymentDAO) {
		this.paymentDAO = paymentDAO;
	}

	public PaymentDAO getPaymentDAO() {
		return paymentDAO;
	}
	
	public List<Payment> getAllPaymentsByUser(String username) throws Exception{
		return paymentDAO.getAllPaymentsByUser(username);
	}

	public List<Payment> getAllPaymentsByTenant(int tenantId) throws Exception{
		return paymentDAO.getAllPaymentsByTenant(tenantId);
	}

	public List<Payment> getAllPayments() throws Exception{
		return paymentDAO.getAllPayments();
	}

	public VelocityEngine getVelocityEngine() {
		return velocityEngine;
	}

	public void setVelocityEngine(VelocityEngine velocityEngine) {
		this.velocityEngine = velocityEngine;
	}

}
